package com.wclass.mapper;

import com.baomidou.mybatisplus.mapper.BaseMapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.pagination.Pagination;
import com.wclass.entity.CouresOrder;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.session.RowBounds;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 课程订单 Mapper 接口
 * </p>
 *
 * @author 张浩高
 * @since 2018-12-21
 */
public interface CouresOrderMapper extends BaseMapper<CouresOrder> {


    /**
     * <p>
     * 根据 entity 条件，查询全部记录（并翻页）
     * </p>
     *
     * @param rowBounds 分页查询条件（可以为 RowBounds.DEFAULT）
     * @param wrapper   实体对象封装操作类（可以为 null）
     * @return List<T>
     */
    @Override
    @Select("select DISTINCT info.*,baseAppUser.nick_name,baseAppUser.picture_address,baseAppUser.user_name,courseInfo" +
            ".cover_img_url,courseInfo.name as course_name,teacher.nick_name as teacher_nick_name,teacher.user_name " +
            "as teacher_user_name ${ew.sqlSelect} from coures_order as info " +
            "left join base_app_user as baseAppUser on info.pay_person_id=baseAppUser.id " +
            "left join course_info as courseInfo on info.course_info_id=courseInfo.id  " +
            "left join course_chapter as chapter on chapter.course_info_id=courseInfo.id  " +
            "left join base_app_user as teacher on info.user_id=teacher.id " +
            "where info.state=0 and baseAppUser.state=0 ${ew.sqlSegment}")
    List<CouresOrder> selectPage(RowBounds rowBounds, @Param("ew") Wrapper<CouresOrder> wrapper);


    /**
     * <p>
     * 根据 ID 查询
     * </p>
     *
     * @param id 主键ID
     * @return T
     */
    @Override
    @Select("select info.*,baseAppUser.nick_name,baseAppUser.picture_address,baseAppUser.user_name,courseInfo" +
            ".cover_img_url,courseInfo.name as course_name,teacher.nick_name as teacher_nick_name,teacher.user_name " +
            "as teacher_user_name  from coures_order as info left join base_app_user as baseAppUser on" +
            " info.pay_person_id=baseAppUser.id left join course_info as courseInfo on info.course_info_id=courseInfo" +
            ".id  left join base_app_user as  teacher  on info.user_id=teacher.id where info.state=0 and baseAppUser" +
            ".state=0 and info.id = #{id}")
    CouresOrder selectById(@Param("id") Serializable id);

    @Override
    @Select("select info.* from coures_order as info " +
            "left join course_info as course on info.course_info_id=course.id " +
            "where 1=1 ${ew.sqlSegment}")
    List<CouresOrder> selectList(@Param("ew") Wrapper<CouresOrder> wrapper);

    @Select("select c.course_info_id as couseInfoId,i.`name`,c.settlement_state,count(*) as num,SUM(c.amount) as totalAmount,SUM(c.serve_money) as totalServeMoney,SUM(c.refund_money) as totalRefundMoney " +
            "from coures_order c left join course_info i on c.course_info_id = i.id " +
            "where 1=1 ${ew.sqlSegment} group by c.course_info_id")
    List<Map<String, Object>> selectCourseSettleCountList(Pagination page, @Param("ew") Wrapper<CouresOrder> wrapper);

    @Select("select distinct info.pay_person_id from coures_order info where info.user_id = #{userId}")
    List<Long> selectTeaStudentList(Pagination page, @Param("userId") Long userId);

    @Select("select distinct info.course_info_id " +
            "from coures_order info " +
            "left join coures_progress_bill process on info.course_info_id=process.course_info_id and process.user_id=#{userId} " +
            "where info.state=0 and info.pay_state=1 and process.id is not null and info.pay_person_id = #{userId}")
    List<Long> selectHasProcessList(Pagination page, @Param("userId") Long userId);

}
